package com.hotacorp.opencloud.jobservice.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.jobservice.entity.ScheduleJobEntity;
import com.hotacorp.opencloud.jobservice.service.ScheduleJobService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



/**
 * 定时任务
 *
 * @author lwg
 * @email liwugang@139.com
 * @date 2020-01-03 12:31:29
 */
@RestController
@Api(tags = "任务管理API")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 列表
     */
    @GetMapping(value = "/schedulejob/list")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:list')")
	@ApiOperation(value = "查询任务")
    @LogAnnotation(module = "任务")
    public Result<?> list(@RequestParam Map<String, Object> params){
        PageData page = scheduleJobService.queryPage(params);

        return Result.succeed("succeed", page);
    }


    /**
     * 信息
     */
    @GetMapping(value = "/schedulejob/info/{jobId}")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:info')")
	@ApiOperation(value = "任务信息")
    @LogAnnotation(module = "任务")
    public Result<?> info(@PathVariable("jobId") Long jobId){
		ScheduleJobEntity scheduleJob = scheduleJobService.getById(jobId);

        return Result.succeed("succeed", scheduleJob);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/schedulejob/save")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:save')")
	@ApiOperation(value = "添加任务")
    @LogAnnotation(module = "任务")
    public Result<?> save(@RequestBody @Validated(AddGroup.class) ScheduleJobEntity scheduleJob){
		scheduleJobService.saveJob(scheduleJob);

        return Result.succeed();
    }

    /**
     * 修改
     */
    @PostMapping(value = "/schedulejob/update")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:update')")
	@ApiOperation(value = "修改任务")
    @LogAnnotation(module = "任务")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) ScheduleJobEntity scheduleJob){
 		scheduleJobService.update(scheduleJob);

        return Result.succeed();
    }

    /**
     * 删除
     */
    @PostMapping(value = "/schedulejob/delete")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:delete')")
	@ApiOperation(value = "删除任务")
    @LogAnnotation(module = "任务")
    public Result<?> delete(@RequestBody Long[] jobIds){
		scheduleJobService.deleteBatch(jobIds);

        return Result.succeed();
    }
    
	/**
	 * 立即执行任务
	 */
	@PostMapping("/schedulejob/run")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:run')")
	@ApiOperation(value = "立即执行任务")
    @LogAnnotation(module = "任务")
	public Result<?> run(@RequestBody Long[] jobIds){
		scheduleJobService.run(jobIds);
		
		return Result.succeed();
	}
	
	/**
	 * 暂停定时任务
	 */
	@PostMapping("/schedulejob/pause")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:pause')")
	@ApiOperation(value = "暂停任务")
    @LogAnnotation(module = "任务")
	public Result<?> pause(@RequestBody Long[] jobIds){
		scheduleJobService.pause(jobIds);
		
		return Result.succeed();
	}
	
	/**
	 * 恢复定时任务
	 */
	@PostMapping("/schedulejob/resume")
    @PreAuthorize("hasAuthority('jobservice:schedulejob:resume')")
	@ApiOperation(value = "恢复任务")
    @LogAnnotation(module = "任务")
	public Result<?> resume(@RequestBody Long[] jobIds){
		scheduleJobService.resume(jobIds);
		
		return Result.succeed();
	}

}
